package com.cgtong.android.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.cgtong.android.R;
import com.cgtong.android.activity.MyDraftActivity;
import com.cgtong.android.activity.ReportProblemActivity;
import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.util.CommonUtils;
import com.cgtong.android.widget.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chunc on 2017/10/18.
 */

public class DraftAdapter extends BaseQuickAdapter<ProblemBean, BaseViewHolder> {


    public DraftAdapter(@Nullable List<ProblemBean> data) {
        super(R.layout.adapter_my_drafts, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ProblemBean item) {

        ImageView avatarImg = helper.getView(R.id.avatar_img);
        if (item.getAvasterId() >= 1 && item.getAvasterId() <= 12) {
            CommonUtils.setUserAvatar(mContext, avatarImg, item.getAvasterId());
        } else {
            LogUtils.d("avatarId错误");
            avatarImg.setImageResource(R.drawable.head_1);
        }

        helper.setText(R.id.name_txt, item.getNickName());
        helper.setText(R.id.date_txt, item.getCreateTime());
        helper.setText(R.id.content_txt, item.getDesc());
        helper.setText(R.id.location_txt, item.getAddress());
        helper.addOnClickListener(R.id.right);
        helper.addOnClickListener(R.id.content);
        //展示图片
        List<ProblemBean.ResourceBean> picList = item.getResourceList();
        GridView gridView = helper.getView(R.id.grid_view);
        if (picList != null && picList.size() > 0) {
            gridView.setVisibility(View.VISIBLE);
            gridView.setAdapter(new ProblemPictureAdapter(picList, item, helper.getLayoutPosition()));
        } else {
            gridView.setVisibility(View.GONE);
        }


        //录音
        if (!TextUtils.isEmpty(item.getVoiceUrl()) && item.getVoiceUrl().startsWith("http")) {
            helper.setGone(R.id.audio_ll, true);
            helper.setText(R.id.play_audio_duration_txt,item.getVoiceTime() + "\"");
        } else {
            helper.setGone(R.id.audio_ll, false);
        }
        //文字
        if (!TextUtils.isEmpty(item.getDesc())) {
            helper.setGone(R.id.content_txt, true);
            helper.setText(R.id.content_txt, item.getDesc());
        } else {
            helper.setGone(R.id.content_txt, false);
        }
    }


    class ProblemPictureAdapter extends BaseAdapter {

        private List<ProblemBean.ResourceBean> mList;
        private ProblemBean mProblemBean;
        private int mPosition;

        public ProblemPictureAdapter(List<ProblemBean.ResourceBean> mList, ProblemBean problemBean, int position) {
            this.mList = mList;
            this.mProblemBean = problemBean;
            this.mPosition = position;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public ProblemBean.ResourceBean getItem(int position) {
            if (position <= mList.size() - 1) {
                return mList.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.layout_picture, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (getItem(position) != null) {
                GlideApp.with(mContext).load(getItem(position).getSmallContent()).dontAnimate()
                        .placeholder(R.drawable.icon_default_loading)
                        .error(R.drawable.icon_default_error).into(holder.imageView);
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        ImagePreviewActivity.start(mContext, mList, position);
                        ReportProblemActivity.startForResult((MyDraftActivity) mContext, true, mProblemBean, mPosition);

                    }
                });
            } else {
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ReportProblemActivity.startForResult((MyDraftActivity) mContext, true, mProblemBean, mPosition);
                    }
                });
            }
            return convertView;
        }


        class ViewHolder {

            @BindView(R.id.img_view)
            ImageView imageView;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

    }
}
